#include "renderframe.h"
#include <QPainter>

RenderFrame::RenderFrame(QWidget *parent)
	: QFrame(parent)
{
	_scale = 1;
}

RenderFrame::~RenderFrame()
{

}

void RenderFrame::paintEvent( QPaintEvent * event )
{
	drawBackground();
	drawAix();
}

void RenderFrame::drawBackground()
{
	int x = 0;
	int y = 0;
	QPainter painter(this);
	QBrush lightGreyBrush(QColor("#3C3C3C"));
	QBrush greyBrush(QColor("#393939"));
	QPen pen(Qt::NoPen);
	painter.setPen(pen);
	bool useGrey = true;
	while(x < width()){
		y = 0;
		if( (x / 8) %2 == 0 ){
			useGrey = true;
		}else{
			useGrey = false;
		}
		while( y < height()){
			if(useGrey){
				painter.setBrush(greyBrush);
			}else{
				painter.setBrush(lightGreyBrush);
			}
			painter.drawRect(x,y,8,8);
			useGrey = !useGrey;
			y += 8;
		}
		x += 8;
	}
}

void RenderFrame::drawAix()
{	
	QPainter painter(this);
	painter.setPen(QColor("white"));
	painter.drawLine(world2Node(QPoint(-worldWidth(),-worldHeight())),world2Node(QPoint(worldWidth(), -worldHeight())));
	painter.drawLine(world2Node(QPoint(-worldWidth(),-worldHeight())),world2Node(QPoint(-worldWidth(), worldHeight())));

	QFont font("times",8);
	painter.setFont(font);
	QFontMetrics fm(font);
	//for(int x = 50/2; x < nodeWidth() + edgeSize; x += 5){
	//	int lineLen = 0;
	//	if(x % 50 == 0){
	//		lineLen = 8;
	//		QString str = QString("%1").arg(node2World(QPoint(x,0)).x());
	//		int yshif = lineLen ;
	//		int xshif = (fm.width(str) / 2);
	//		painter.drawText(QPoint(x - xshif ,nodeHeight() + lineLen + yshif),str);
	//	}else if(x % 10 == 0){
	//		lineLen = 5;
	//	}else{
	//		lineLen = 3;
	//	}
	//	painter.drawLine(QPoint(x,nodeHeight()),QPoint(x,nodeHeight() + lineLen));
	//	
	//}
	//for(int y = 0; y < nodeHeight(); y += 5){
	//	int lineLen = 0;
	//	if(y % 50 == 0){
	//		lineLen = 8;
	//		QString str = QString("%1").arg(node2World(QPoint(0,y)).y());;
	//		int yshif = lineLen ;
	//		int xshif = (fm.width(str) / 2);
	//		painter.save();
	//		painter.translate(QPoint(edgeSize - lineLen - yshif,y - xshif));
	//		painter.rotate(90); // or 270
	//		painter.drawText( 0, 0, str);
	//		painter.restore();

	//	}else if(y % 10 == 0){
	//		lineLen = 5;
	//	}else{
	//		lineLen = 3;
	//	}
	//	painter.drawLine(QPoint(edgeSize,y),QPoint(edgeSize - lineLen,y));
	//}
	for(int x = ((-worldWidth() + 5)/5)*5; x < worldWidth(); x += 5){
		int lineLen = 0;
		if(x % 50 == 0){
			lineLen = 8 / _scale;
			QString str = QString("%1").arg(x);
			int yshif = lineLen ;
			int xshif = (fm.width(str) / 2) /_scale;
			painter.drawText(world2Node(QPoint(x - xshif ,-worldHeight() - lineLen - yshif)),str);
		}else if(x % 10 == 0){
			lineLen = 5 / _scale;
		}else{
			lineLen = 3 / _scale;
		}
		painter.drawLine(world2Node(QPoint(x,-worldHeight())),world2Node(QPoint(x,-worldHeight() - lineLen)));
	}
	for(int y = ((-worldHeight() + 5)/5) * 5; y < worldHeight(); y += 5){
		int lineLen = 0;
		if(y % 50 == 0){
			lineLen = 8 / _scale;
			QString str = QString("%1").arg(y);
			int yshif = lineLen ;
			int xshif = (fm.width(str) / 2) /_scale;
			painter.save();
			painter.translate(world2Node(QPoint(-worldWidth() - lineLen - yshif,y + xshif)));
			painter.rotate(90); // or 270
			painter.drawText( 0, 0, str);
			painter.restore();

		}else if(y % 10 == 0){
			lineLen = 5 / _scale;
		}else{
			lineLen = 3 / _scale;
		}
		painter.drawLine(world2Node(QPoint(-worldWidth(),y)),world2Node(QPoint(-worldWidth() - lineLen,y)));
	}
	painter.setPen(QColor("grey"));
	painter.drawLine(world2Node(QPoint(0,-worldHeight())),world2Node(QPoint(0, worldHeight())));
	painter.drawLine(world2Node(QPoint(-worldWidth(),0)),world2Node(QPoint(worldWidth(), 0)));
}

QPoint RenderFrame::world2Node( const QPoint &pos )
{
	QPoint nodePos = QPoint( pos.x() * _scale + nodeWidth()/2 + edgeSize,  -pos.y() * _scale + nodeHeight()/2);
	return nodePos;
}

QPoint RenderFrame::node2World( const QPoint &pos )
{
	QPoint worldPos = QPoint((pos.x() - edgeSize - nodeWidth()/2 ) / _scale,  -(pos.y() - nodeHeight()/2) / _scale);
	return worldPos;
}

int RenderFrame::nodeHeight()
{
	return height() - edgeSize;
}

int RenderFrame::nodeWidth()
{
	return width() - edgeSize;
}

int RenderFrame::worldHeight()
{
	return ((height() - edgeSize) / _scale) /2;
}

int RenderFrame::worldWidth()
{
	return ((width() - edgeSize) / _scale) / 2;
}

double RenderFrame::scale()
{
	return _scale;
}

void RenderFrame::setScale( double s )
{
	_scale = s;
}

void RenderFrame::mousePressEvent( QMouseEvent * event )
{

}
